package com.three.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.JsonObject;
import com.three.dao.AnnounceDao;
import com.three.dao.ClazzDao;
import com.three.dao.SRrecordDao;
import com.three.dao.SrollcallDao;
import com.three.entity.Clazz;
import com.three.entity.SRrecord;
import com.three.service.ClazzService;
import com.three.util.GenCodes;

@Service
public class ClazzServiceImpl implements ClazzService {

	@Autowired
	ClazzDao clazzDao;
	@Autowired
	AnnounceDao announceDao;
	@Autowired
	SrollcallDao srollcallDao;
	@Autowired
	SRrecordDao sRrecordDao;

	
	@Override
	public List<Clazz> findClassesByUid(Integer id) {
		List<Clazz> classes = new ArrayList<Clazz>();
		classes = clazzDao.findClassesByUid(id);
		return classes;
	}

	@Override
	public String resetInviteCode(Integer cid) {
		String result = null;
		String inviteCode = GenCodes.genSixRandomCodes();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("cid", cid);
		map.put("inviteCode", inviteCode);
		int state = clazzDao.updateInviteCode(map);
		if (state>0) {			
			result=inviteCode;
		}else{
			result = "failed";
		}
		return result;
	}

	@Override
	public String stopInviteCode(Integer cid) {
		String result = null;
		int state = clazzDao.stopInviteCode(cid);
		if (state>0) {
			result="stopped";
		}else{
			result="failed";
		}
		
		return result;
	}

	@Override
	public JSONObject createClazz(String clazzName,
			Integer uid,
			String tecName) {
		String inviteCode = GenCodes.genSixRandomCodes();
		clazzDao.addClazz(clazzName, inviteCode, tecName);
		int cid = clazzDao.findLastInsertId();
		clazzDao.addAssoUC(uid, cid);
		Clazz clazz = clazzDao.findClazzByCid(cid);
		JSONObject obj = new JSONObject();
		obj.put("code", "success");
		obj.put("clazz", clazz);
		return obj;
	}

	@Override
	public String updateClazz(Integer cid, String name) {
		String result = null;
		int state = clazzDao.updateName(cid, name);
		if (state>0) {
			result = "success";
		}else{
			result = "failed";
		}
		return result;
	}

	@Override
	public String deleteClazz(Integer cid) {
		String result = null;
		List<Integer> ucids = clazzDao.findUcidsByCid(cid);
		if (ucids.size()>0) {
			sRrecordDao.deleteSRrecordsByUcids(ucids);
			srollcallDao.deleteSrollcallsByUcids(ucids);
		}
		clazzDao.deleteAssoUC(cid);
		announceDao.deleteAnnouncesByCid(cid);
		clazzDao.deleteClazz(cid);
		result="success";
		return result;
	}

	/**
	 * 不存在班级 code:"notexist"
	 * 已加入班级 code:"joined"
	 * 成功加入 code:"success" + clazz实体json
	 * 其他 code:"failed"
	 */
	@Override
	public JSONObject joinClazz(Integer uid, String inviteCode) {
		JSONObject obj= new JSONObject();
		List<Integer> cids = clazzDao.findCidByInviteCode(inviteCode);
		int cid = cids.get(0);
		if (cids.size()>0) {
			//存在该班级，判断是否已经加入
			List<Integer> ucids = clazzDao.findUcidByUidWithCid(uid, cid);
			if (ucids.size() >0) {
				//已经加入该班级
				obj.put("code", "joined");
			}else{
				//未加入该班级，执行加入班级操作
				int state = clazzDao.addAssoUC(uid, cid);
				List<Integer> ucids2 = clazzDao.findUcidByUidWithCid(uid, cid);
				if (state>0) {
					//加入班级成功，查询班级信息并返回
					obj.put("code", "success");
					//向s_rollcall表插入初始化数据
					srollcallDao.addDefaultData(ucids2.get(0));
					Clazz clazz = clazzDao.findClazzByCid(cid);
					obj.put("clazz", clazz);
				}else{
					//加入班级失败
					obj.put("code", "failed");
				}
			}
			
		}else{
			//不存在该班级
			obj.put("code", "notexist");
		}
		return obj;
	}

	/**
	 * 退出班级
	 */
	@Override
	public String quitClazz(Integer uid, Integer cid) {
		String result = null;
		List<Integer> ucids = clazzDao.findUcidByUidWithCid(uid, cid);
		int ucid = ucids.get(0);
		srollcallDao.deleteSrollcallByUcid(ucid);
		sRrecordDao.deleteSRrecordsByUcid(ucid);
		clazzDao.deleteAssoUCByUcid(ucid);
		result = "success";
		return result;
	}

}
